<template>
  <div>
    <n-card :bordered="false" class="proCard">
      <BasicForm
        :showAdvancedButton="false"
        @register="register"
        @submit="handleSubmit"
        @reset="handleReset"
      >
        <template #statusSlot="{ model, field }">
          <n-input v-model:value="model[field]" />
        </template>
      </BasicForm>

      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row: ListData) => row.id"
        ref="actionRef"
        :actionColumn="actionColumn"
        @update:checked-row-keys="onCheckedRow"
        :scroll-x="1090"
      >
        <template #tableTitle>
          <n-button type="primary" @click="handleExport"> 导出 </n-button>
        </template>

        <template #toolbar>
          <n-button type="primary" @click="reloadTable">刷新数据</n-button>
        </template>
      </BasicTable>

      <n-modal v-model:show="showModal" :show-icon="false" preset="dialog" title="新建">
        <n-form
          :model="formParams"
          :rules="rules"
          ref="formRef"
          label-placement="left"
          :label-width="80"
          class="py-4"
        >
          <n-form-item label="群聊名称" path="name">
            <n-input placeholder="群聊名称" v-model:value="formParams.name" disabled />
          </n-form-item>
          <n-form-item label="出行地点" path="name">
            <n-input
              disabled
              type="textarea"
              placeholder="群聊名称"
              v-model:value="formParams.name"
            />
          </n-form-item>
          <n-form-item label="出行成员" path="name">
            <n-input
              disabled
              type="textarea"
              placeholder="群聊名称"
              v-model:value="formParams.name"
            />
          </n-form-item>
          <n-form-item label="文案信息" path="name">
            <n-input
              disabled
              type="textarea"
              placeholder="群聊名称"
              v-model:value="formParams.name"
            />
          </n-form-item>
        </n-form>

        <template #action>
          <n-space>
            <n-button @click="() => (showModal = false)">取消</n-button>
            <n-button type="info" :loading="formBtnLoading" @click="confirmForm">确定</n-button>
          </n-space>
        </template>
      </n-modal>
    </n-card>
  </div>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { getTableList } from '@/api/table/list';
  import { columns, ListData } from './columns';
  import { PlusOutlined } from '@vicons/antd';
  import { useRouter } from 'vue-router';
  import { type FormRules } from 'naive-ui';
  import { exData } from '@/utils/exData';
  const rules: FormRules = {
    name: {
      trigger: ['blur', 'input'],
      message: '请输入名称',
    },
    role: {
      type: 'number',
      message: '请选择所属角色',
      trigger: 'change',
    },
  };
  const roleList = [
    {
      label: '超级管理员',
      value: 1,
    },
    {
      label: '普通管理员',
      value: 2,
    },
  ];

  const schemas: FormSchema[] = [
    {
      field: 'makeDate',
      component: 'NDatePicker',
      label: '出行时间',
      componentProps: {
        placeholder: '请选择',

        type: 'date',
        clearable: true,
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'mobile',
      component: 'NInputNumber',
      label: '出行次数',
      componentProps: {
        placeholder: '请输入出行次数',
        showButton: false,
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'name',
      component: 'NInput',
      label: '群昵称',
      componentProps: {
        placeholder: '请输入群昵称',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
  ];

  const router = useRouter();
  const formRef: any = ref(null);
  const actionRef = ref();

  const showModal = ref(false);
  const formBtnLoading = ref(false);
  const formParams = reactive({
    name: '',
    role: null,
    droit: true,
  });
  const handleExport = () => {
    exData(document.querySelector('.n-data-table-base-table'), '表');
  };
  const actionColumn = reactive({
    width: 220,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'text',
        actions: [
          {
            label: '查看详情',
            onClick: handleShowDetail.bind(null, record),
            // 根据业务控制是否显示 isShow 和 auth 是并且关系
            ifShow: () => {
              return true;
            },
            // 根据权限控制是否显示: 有权限，会显示，支持多个
            auth: ['basic_list'],
          },
        ],
      });
    },
  });

  const [register, { getFieldsValue }] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  function handleShowDetail() {
    showModal.value = true;
  }
  // 滑块切换添加权限状态
  function handleChange(value: boolean) {
    console.log(value);
  }

  const loadDataTable = async (res) => {
    return await getTableList({ ...getFieldsValue(), ...res });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function confirmForm(e) {
    e.preventDefault();
    formBtnLoading.value = true;
    formRef.value.validate((errors) => {
      if (!errors) {
        window['$message'].success('新建成功');
        setTimeout(() => {
          showModal.value = false;
          reloadTable();
        });
      } else {
        window['$message'].error('请填写完整信息');
      }
      formBtnLoading.value = false;
    });
  }

  // function handleShowDetail(record: Recordable) {
  //   window['$message'].info('查看详情');
  // }

  function handleSubmit(values: Recordable) {
    console.log(values);
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }
</script>

<style lang="less" scoped></style>
